﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Jzy.Model.Project;

using Jzy.DAL.Project;
using System.Data;

using Frame.Lib.Core.Tree;
using Frame.Lib.Core.Attach;
using Frame.Lib.Enums;

namespace Jzy.BLL.Project
{
    public class PrjTaskBO:TreeBO<PrjTaskInfo>
    {
        private int _prjID;
        private PrjTaskDAL _task;
        public PrjTaskBO(int prjID)
            : base("PrjTask")
        {
            _prjID = prjID;
            _task = new PrjTaskDAL(prjID);
            _task.dbOper = base.dbOper;
        }

        /// <summary>
        /// 删除以后处理时间，进度等问题
        /// </summary>
        /// <param name="ids"></param>
        public override void Delete(IEnumerable<int> ids)
        {
            List<int> subIDs = base.GetSubIDs(ids);//没有去过重
            
            try
            {
                base.dbOper.BeginTrans();
                foreach (int id in subIDs)
                {
                    PrjTaskInfo info = base.GetInfo(id);
                    if (info != null)
                    {
                        this.Delete(id);
                        SetStatistic(info);
                    }
                }
                base.dbOper.CommitTrans();
                   
            }
            catch(Exception ex)
            {
                base.dbOper.RollbackTrans();
                throw ex;
            }
         
            
        }

        public override void Delete(int id)
        {
            AttachBO attach = new AttachBO("PrjTaskAttach");
           
            attach.DeleteByBindID(id);

            base.Delete(id);
        }


        public override void Save(PrjTaskInfo info)
        {
            base.Save(info);

            SetStatistic(info);
        }

        private void SetStatistic(PrjTaskInfo info)
        {
            if (info.UpperID != Constants.TREE_ROOT_UPPER_ID)
            {
                PrjTaskInfo parentInfo = base.GetInfo(info.UpperID);
                if (parentInfo != null)
                {
                    _task.SetStatistic(parentInfo);
                    base.Save(parentInfo);

                    SetStatistic(parentInfo);
                }

            }
            else
            {
                if (info.Percent == 100)//更改项目状态为完成
                {
                    
                    ProjectBO prj = new ProjectBO();
                    ProjectInfo prjInfo = prj.GetInfo(_prjID);
                    if (prjInfo.Status == (int)PrjStatus.启动)
                    {
                        prj.SetStatus(_prjID, PrjStatus.结束);
                    }
                }
            }
        }

        public override List<PrjTaskInfo> GetTreeList(int rootID)
        {
            List<PrjTaskInfo> list = base.GetTreeList(rootID);

            foreach (PrjTaskInfo info in list)
            {
                if (info.EndTime < DateTime.Now && info.Percent < 100)
                {
                    info.flag = "gw_red.gif";
                }
                else
                {
                    info.flag = "gw_green.gif";
                }
            }


            return list;
        }        

        public override int GetRootID()
        {
            object taskID = _task.GetRootID();
            return (int)taskID;
        }

        public void MoveRight(object taskID)
        {
            PrjTaskInfo info = base.GetInfo(taskID);
            //判断在同级当中排序是不是最小的，如是则不做操作
            List<PrjTaskInfo> list = base.GetList(info.UpperID).ToList();
            int minOrders = list.First().Orders;
            if (info.Orders == minOrders)
            {
                return;
            }

            //把自己移到它上面同级下
            int destID = list.Where(data => data.Orders < info.Orders).Last().ID;//将要移动的目标
            List<int> lstSrcIDs = new List<int>(1);
            lstSrcIDs.Add(info.ID);

            base.Move(lstSrcIDs, destID);
            
        }

        public void MoveLeft(object taskID)
        {
            PrjTaskInfo info = base.GetInfo(taskID);
            if (info.Depth == 1)//已经到顶了
            {
                return;
            }
           
            IEnumerable<PrjTaskInfo> list = base.GetList(info.UpperID);//获得同级的所有列表
            List<int> ids = list.Where(data => data.Orders > info.Orders).Select(data => data.ID).ToList();//取得排在自己后面的同级ID
         

            //先把自己往上移一级
            List<int> lstSrcIDs = new List<int>(1);
            lstSrcIDs.Add(info.ID);

            PrjTaskInfo parent = base.GetInfo(info.UpperID);
            base.Move(lstSrcIDs, parent.UpperID);

            //再把原来自己后面的移到自己的下一级
            base.Move(ids, info.ID);

        }

        public void MoveDown(object taskID)
        {
            PrjTaskInfo info = base.GetInfo(taskID);

            //找到同级的下一个

            IEnumerable<PrjTaskInfo> nextInfos = base.GetList(info.UpperID).Where(data => data.Orders > info.Orders);
            if (nextInfos.Count() > 0)
            {
                PrjTaskInfo next = nextInfos.First();

                List<int> ids = new List<int>(2);
                List<int> values = new List<int>(2);

                ids.Add(info.ID);
                ids.Add(next.ID);

                values.Add(next.Orders);
                values.Add(info.Orders);

                base.SaveOrders(ids, values);
            }
        }

        

        public void MoveUp(object taskID)
        {
            PrjTaskInfo info = base.GetInfo(taskID);

            //找到同级的下一个

            IEnumerable<PrjTaskInfo> nextInfos = base.GetList(info.UpperID).Where(data => data.Orders < info.Orders);
            if (nextInfos.Count() > 0)
            {
                PrjTaskInfo pre = nextInfos.Last();

                List<int> ids = new List<int>(2);
                List<int> values = new List<int>(2);

                ids.Add(info.ID);
                ids.Add(pre.ID);

                values.Add(pre.Orders);
                values.Add(info.Orders);
                base.SaveOrders(ids, values);
            }
        }



        public List<int> GetIndependentRoot(List<int> ids, List<PrjTaskInfo> dt)
        {
            List<int> lstReturn = new List<int>();

            IEnumerable<PrjTaskInfo> rows = from row in dt
                                            where ids.Contains(row.ID)
                                            select row;
            foreach (PrjTaskInfo row in rows)
            {
                //一条记录的Path里只要包含了其它的ID，就证明它不是独立的根
                int count = rows.Where(data => row.Path.Contains("," + data.ID + ",")).Count();

                if (count == 0)
                {
                    lstReturn.Add(row.ID);
                }
            }
            return lstReturn;
        }


        public void MoveLeft(List<int> ids, List<PrjTaskInfo> TmpDt)
        {
            List<int> lstIDs = GetIndependentRoot(ids, TmpDt);
            //把顺序重排一下

            ReSort(TmpDt);


            foreach (int id in lstIDs)
            {
                MoveLeft(id);
            }
        }

        private void ReSort(List<PrjTaskInfo> list)
        {
            List<int> allIDs = new List<int>(list.Count);
            List<int> allValues = new List<int>(list.Count);

            foreach (PrjTaskInfo info in list)
            {
                allIDs.Add(info.ID);
                allValues.Add(info.Orders);
            }          

            allValues.Sort();

            base.SaveOrders(allIDs, allValues);
        }


        public void MoveRight(List<int> ids, List<PrjTaskInfo> list)
        {
            List<int> lstIDs = GetIndependentRoot(ids, list);

            //把顺序重排一下

            ReSort(list);

            foreach (int id in lstIDs)
            {
                MoveRight(id);
            }
        }

        public void MoveUp(List<int> ids, List<PrjTaskInfo> list)
        {
            List<int> lstIDs = GetIndependentRoot(ids, list);
            foreach (int id in lstIDs)
            {
                MoveUp(id);
            }
        }

        public void MoveDown(List<int> ids, List<PrjTaskInfo> list)
        {
            List<int> lstIDs = GetIndependentRoot(ids, list);
            for (int i = lstIDs.Count - 1; i >= 0; i--)
            {
                MoveDown(lstIDs[i]);
            }           
        }
    }
}
